package com.alibaba.nacos.common.task.engine;

import com.alibaba.nacos.common.task.NacosTaskProcessor;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

@Setter
public abstract class AbstractNacosTaskExecuteEngine<T>
        implements NacosTaskExecuteEngine<T> {

    @Setter
    private NacosTaskProcessor defaultTaskProcessor;
    @Getter
    private final Map<Object, NacosTaskProcessor> taskProcessors = new ConcurrentHashMap<>(32);


    @Override
    public void addProcessor(Object key,
                             NacosTaskProcessor processor) {
        taskProcessors.putIfAbsent(key, processor);
    }

    @Override
    public NacosTaskProcessor getProcessor(Object key) {
        return Optional.ofNullable(taskProcessors.get(key)).orElse(defaultTaskProcessor);
    }

}
